//
//  newsDetailVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/5/17.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "newsDetailVC.h"

@interface newsDetailVC ()<UIWebViewDelegate>
@property (strong, nonatomic)UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@end

@implementation newsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = self.titleStr;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    //view不需要拓展到整个屏幕
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.webView.delegate = self;
//    self.scrollview.showsHorizontalScrollIndicator = NO;
//    self.scrollview.showsVerticalScrollIndicator = NO;
    self.scrollview.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    
    
    UIWebView *webview = [[UIWebView alloc] init];
    webview.size = [UIScreen mainScreen].bounds.size;
    webview.x = 0;
    webview.y = 0;
    webview.width = screenW;
//    webview.scrollView.bounces = NO;
//    webview.scrollView.showsHorizontalScrollIndicator = NO;
//    webview.scrollView.showsVerticalScrollIndicator = NO;
    webview.delegate = self;
//    [self.scrollview addSubview:webview];
    self.webView = webview;
    [self.view addSubview: self.webView];
    NSURL *url = [NSURL URLWithString:_urlStr];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    self.webView.scalesPageToFit = YES;
    self.webView.hidden = YES;
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(-50, 0, 60, 0);
    [self.webView loadRequest:urlRequest];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
}

-(void)leftBarButtonItemClick{

    [self.navigationController popViewControllerAnimated:YES];
}
-(void)webViewDidFinishLoad:(UIWebView
                             
                             *)webView{
    

    NSInteger height =[[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];
    //NSLog(@"inter:%ld",(long)height);
    if (height ==  webView.height - 16) {
        //没有数据的时候，UILabel的显示样式;
        //        UILabel *messageLabel = [UILabel new];
        //        messageLabel.text = message;
        //        messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        //        messageLabel.textColor = [UIColor lightGrayColor];
        //        messageLabel.textAlignment = NSTextAlignmentCenter;
        //        [messageLabel sizeToFit];
        //
        //        self.backgroundView = messageLabel;
        UIImage *imgNo = [UIImage imageNamed:@"加载失败"];
        UIImageView *imgV = [[UIImageView alloc] initWithImage:imgNo];
        //        imgV.frame = (CGRect){(320-168)/2,(self.frame.size.height-130)/2,168,130};
        imgV.center = self.webView.center;
//        imgV.y = self.webView.height/2 + imgV.height;
        [self.webView addSubview:imgV];
        
        
        //        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
//    CGFloat
//    
//    webViewHeight=
//    
//    [[webView
//      
//      stringByEvaluatingJavaScriptFromString:
//      
//      @"document.body.offsetHeight"]floatValue];
//    
//    
//    
//    CGRect
//    
//    newFrame 
//    = 
//    webView.frame;
//    
//    
//    
//    newFrame.size.height
//    
//    =
//    webViewHeight;
//    
//    
//    
//    webView.frame
//    
//    = 
//    newFrame;
//    
//    self.webView.height = newFrame.size.height + 50;
//    self.webView.userInteractionEnabled = NO;
//    self.scrollview.contentSize = CGSizeMake(0, newFrame.size.height + 50);
//
    //处理“下载app的广告”
    NSString *downloadHeaderJSStr = @"document.getElementById(\"top-app-layer\").style.visibility=\"hidden\"";
    [webView stringByEvaluatingJavaScriptFromString:downloadHeaderJSStr];
    [self performSelector:@selector(btnDismiss) withObject:nil afterDelay:0.4f];
    
}

- (void)btnDismiss{
    self.webView.hidden = NO;
}
@end
